package com.oracle.zebrafreelytraveling.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputEditText;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.Button;

import com.oracle.zebrafreelytraveling.R;
import com.oracle.zebrafreelytraveling.data.RequestResult.ResState;
import com.oracle.zebrafreelytraveling.net.HttpManager;
import com.oracle.zebrafreelytraveling.utils.constant.Constant;
import com.oracle.zebrafreelytraveling.utils.ToastUtil;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

import static com.oracle.zebrafreelytraveling.R.id.nickname;

/**
 * Created by yydcnjjw on 2017/7/29.
 */

public class LogupActivity extends BaseActitvity implements TextWatcher {

    private TextInputEditText mAccount;
    private TextInputEditText mNickName;
    private TextInputEditText mPassword;
    private TextInputEditText mConfirm_password;
    private TextInputEditText mTel;
    private TextInputEditText mE_mail;
    private Button mLogup;

    private Disposable mDisposable;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logup);
        initView();
    }

    private void initView(){
        mAccount = (TextInputEditText) findViewById(R.id.account);
        mNickName = (TextInputEditText) findViewById(nickname);
        mPassword = (TextInputEditText) findViewById(R.id.password);
        mConfirm_password = (TextInputEditText) findViewById(R.id.confirm_password);
        mTel = (TextInputEditText) findViewById(R.id.tel);
        mE_mail = (TextInputEditText) findViewById(R.id.e_mail);
        mLogup = (Button) findViewById(R.id.logup);

        mPassword.addTextChangedListener(this);
        mConfirm_password.addTextChangedListener(this);
        mLogup.setOnClickListener(v -> logup());
    }

    private void logup(){
        mLogup.setClickable(false);
        String account = mAccount.getText().toString();
        String nickname = mNickName.getText().toString();
        String password = mPassword.getText().toString();
        String confirmPassword = mConfirm_password.getText().toString();
        String email = mE_mail.getText().toString();
        String tel = mTel.getText().toString();

        if (!password.equals(confirmPassword)){
            mPassword.setError("密码不相同");
            mConfirm_password.setError("密码不相同");
            return;
        }
        if (account.length() < 0){
            ToastUtil.showToast(getString(R.string.not_input_account));
            return;
        }

        HttpManager.getInstance()
                .getService(Constant.BASE_URL)
                .getResLogUP(account, password, nickname, tel, email)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResState>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mDisposable = d;
                    }
                    @Override
                    public void onNext(@NonNull ResState resState) {
                        ToastUtil.showToast(resState.getMessage());
                        if (resState.getCode().equals(Constant.REQUEST_FAULT)){
                            mLogup.setClickable(true);
                        }
                    }
                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.d("error", e.getMessage());
                        mLogup.setClickable(true);
                    }
                    @Override
                    public void onComplete() {
                        finish();
                    }
                });

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_logup;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (!mPassword.toString().equals(mConfirm_password.toString())){
            mPassword.setError("密码不相同");
            mConfirm_password.setError("密码不相同");
        } else {
            mConfirm_password.setEnabled(false);
            mPassword.setEnabled(false);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mDisposable != null && !mDisposable.isDisposed()) {
            mDisposable.dispose();
        }
    }

    public static void startLogupActivity(Activity activity){
        Intent i = new Intent(activity, LogupActivity.class);
        activity.startActivity(i);
    }
}
